package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Activity;
import com.qk.service.ActivityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/activities")
@RestController
@Slf4j
public class ActivityController {

    @Autowired
    private ActivityService activityService;
    /**
     *  活动列表查询
     */
    @GetMapping
    public Result list(Integer channel , Integer type, Integer status,

                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize){
        log.info("活动列表: channel={},type={},status={},page={},pageSize={}", channel, type, status, page, pageSize );
        PageResult<Activity> pageResult = activityService.list(channel, type, status, page, pageSize);
        return Result.success(pageResult);
    }

    /**
     *  添加活动
     */
    @Log
    @PostMapping
    public Result add(@RequestBody Activity activity){
        log.info("添加活动: {}", activity);
        activityService.add(activity);
        return Result.success();
    }

    /**
     *  根据id查询活动
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
        log.info("查询id：{}",id);
        Activity activity = activityService.selectById(id);
        return Result.success(activity);
    }
    /**
     *  删除活动，根据id
     */
    @Log
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        log.info("删除活动：{}", id);
        activityService.deleteById(id);
        return Result.success();
    }
    /**
     *  修改活动
     */
    @Log
    @PutMapping
    public Result update(@RequestBody Activity activity){
        log.info("修改活动：{}",activity);
        activityService.update(activity);
        return Result.success();
    }
    /**
     *  查询所有活动
     */
    @GetMapping("/list")
    public Result selectAll(){
        log.info("查询所有活动");
        List<Activity> activityList = activityService.selectAll();
        return Result.success(activityList);
    }


    /**
     *  查询指定类型的活动
     */
    @GetMapping("/type/{type}")
    public Result selectByType(@PathVariable Integer type){
        log.info("查询类型：{}", type);
        List<Activity> list = activityService.selectByType(type);
        return Result.success(list);
    }

}
